//
//  WalletViewController.m
//  ForestPack
//
//  Created by 郑洲 on 2018/7/6.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "WalletViewController.h"
#import "BankTableViewCell.h"
#import "BankCardModel.h"
#import "WaterRecordController.h"

@interface WalletViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UILabel           *allMoney;
@property (nonatomic, strong) BaseTableView     *tableView;
@property (nonatomic, strong) NSMutableArray    *BankCardData;

@end

@implementation WalletViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, -STATUS_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT + STATUS_BAR_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.rowHeight = ScreenX375(56);
    [self.view addSubview:self.tableView];
    
    [self setHead];
    
    OperateButton *button = [[OperateButton alloc] initWithFrame:CGRectMake(ScreenX375(13), SCREEN_HEIGHT - ScreenX375(99), SCREEN_WIDTH - ScreenX375(26), ScreenX375(44))];
    [button setTitle:@" 添加银行卡" forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"+副本"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    _BankCardData = [NSMutableArray array];
    [self getData];
}

- (void)setHead {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(200) + STATUS_BAR_HEIGHT)];
    _tableView.tableHeaderView = headView;
    
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(170) + STATUS_BAR_HEIGHT)];
    bgView.image = [UIImage imageNamed:@"背景2"];
    [headView addSubview:bgView];
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT, NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT)];
    [backBtn setImage:[UIImage imageNamed:@"返回白色"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:backBtn];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(60), STATUS_BAR_HEIGHT, SCREEN_WIDTH - ScreenX375(120), NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT)];
    titleLabel.text = @"我的钱包";
    titleLabel.font = Kfont(16);
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:titleLabel];
    
    UIButton *detailBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 44, STATUS_BAR_HEIGHT, 44, 44)];
    [detailBtn setImage:[UIImage imageNamed:@"明细"] forState:UIControlStateNormal];
    [detailBtn addTarget:self action:@selector(turnToRecords) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:detailBtn];

    _allMoney = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(49) + STATUS_BAR_HEIGHT, SCREEN_WIDTH, ScreenX375(44))];
    _allMoney.font = Kfont(30);
    _allMoney.textAlignment = NSTextAlignmentCenter;
    _allMoney.textColor = [UIColor whiteColor];
    [headView addSubview:_allMoney];
    
    UIView *style = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(103) + STATUS_BAR_HEIGHT, SCREEN_WIDTH - ScreenX375(26), ScreenX375(88))];
    style.layer.cornerRadius = 5;
    [headView addSubview:style];
    
    CALayer *shadowLayer0 = [[CALayer alloc] init];
    shadowLayer0.frame = style.bounds;
    shadowLayer0.shadowColor = [UIColor colorWithRed:173.0f/255.0f green:0.0f/255.0f blue:21.0f/255.0f alpha:0.5f].CGColor;
    shadowLayer0.shadowOpacity = 1;
    shadowLayer0.shadowOffset = CGSizeMake(0, 0);
    shadowLayer0.shadowRadius = 3;
    CGFloat shadowSize0 = 1;
    CGRect shadowSpreadRect0 = CGRectMake(-shadowSize0, -shadowSize0, style.bounds.size.width+shadowSize0*2, style.bounds.size.height+shadowSize0*2);
    CGFloat shadowSpreadRadius0 =  style.layer.cornerRadius == 0 ? 0 : style.layer.cornerRadius+shadowSize0;
    UIBezierPath *shadowPath0 = [UIBezierPath bezierPathWithRoundedRect:shadowSpreadRect0 cornerRadius:shadowSpreadRadius0];
    shadowLayer0.shadowPath = shadowPath0.CGPath;
    [style.layer addSublayer:shadowLayer0];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(13), ScreenX375(103) + STATUS_BAR_HEIGHT, SCREEN_WIDTH - ScreenX375(26), ScreenX375(88))];
    view.layer.cornerRadius = 5;
    view.backgroundColor = [UIColor whiteColor];
    [headView addSubview:view];
    
    NSArray *nameArr = @[@"充值",@"提现"];
    for (int i = 0; i < nameArr.count; i++) {
        UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenX375(77) + i * WIDTH(view) / 2, ScreenX375(19), ScreenX375(28), ScreenX375(28))];
        icon.image = [UIImage imageNamed:nameArr[i]];
        [view addSubview:icon];
        
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(76) + i * WIDTH(view) / 2, ScreenX375(54), ScreenX375(30), ScreenX375(16))];
        nameLabel.text = nameArr[i];
        nameLabel.textColor = REXADECIMALCOLOR(@"#333333");
        nameLabel.font = Kfont(14);
        nameLabel.textAlignment = NSTextAlignmentCenter;
        [view addSubview:nameLabel];
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(i * WIDTH(view) / 2, 0, WIDTH(view) / 2, ScreenX375(88))];
        button.backgroundColor = [UIColor clearColor];
        [view addSubview:button];
    }
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(WIDTH(view) / 2, ScreenX375(15), 0.5, ScreenX375(58))];
    line.backgroundColor = LineColor;
    [view addSubview:line];
}
#pragma -- tableview

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _BankCardData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScreenX375(150);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BankTableViewCell *cell = [BankTableViewCell cellWithTableView:tableView andIndexPath:indexPath];
    BankCardModel *model = [_BankCardData objectAtIndex:indexPath.row];
    [cell initWithModel:model];
    return cell;
}

- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)click{
    NSLog(@"clicked");
}

-(void)getData{
    NSDictionary *parameter = @{@"token":kNSUDefaultReadKey(@"token")};
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@app/wallet_index",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"status"]];
        if ([status isEqualToString:@"1"]) {
            NSLog(@"%@",successResponse);
            NSError *error = nil;
            NSString *money = [NSString stringWithFormat:@"￥%@",successResponse[@"user"][@"wallet"]];
            self.allMoney.text = money;
            for(NSDictionary *dict in successResponse[@"data"]){
                BankCardModel *model = [[BankCardModel alloc] initWithDictionary:dict error:&error];
                [self.BankCardData addObject:model];
            }
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

-(void)turnToRecords{
    WaterRecordController *vc = [[WaterRecordController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
